<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-radio-group v-model="tabPosition" class="tabTop" @change="handleRadioChange">
        <el-radio-button label="">
          <div class="tabItem">全部工单</div>
        </el-radio-button>
        <el-radio-button :label="100">
          <div class="tabItem">待派单</div>
        </el-radio-button>
        <el-radio-button :label="200">
          <div class="tabItem">处理中</div>
        </el-radio-button>
        <el-radio-button :label="300">
          <div class="tabItem">办结</div>
        </el-radio-button>
      </el-radio-group>
      <el-form :inline="true" :model="queryParams" size="small" label-width="84px">
        <el-form-item label="工单类型" :label-width="labelWidth" prop="status">
          <el-select v-model="queryParams.typeNo" placeholder="请选择工单类型" clearable>
            <el-option
              v-for="item in workOrderTypeData"
              :key="item.typeNo"
              :label="item.typeName"
              :value="item.typeNo"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="工单状态" :label-width="labelWidth" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="item in workOrderStatusData"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="工单编号" :label-width="labelWidth">
          <el-input v-model="queryParams.no" placeholder="请选择工单编号" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="checkAuthority('/select')" type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column prop="no" label="工单编号" width="160px"></el-table-column>
        <el-table-column prop="paramNo" label="工单来源" :formatter="formatParam"> </el-table-column>
        <el-table-column prop="emergencyLevel" label="紧急程度" width="130px">
          <template slot-scope="scope">
            <el-rate v-model="scope.row.emergencyLevel" :disabled="disabled"> </el-rate>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="当前状态" width="80px" :formatter="formatStatus"></el-table-column>
        <el-table-column prop="typeName" label="工单类型"></el-table-column>
        <el-table-column prop="extAttr2" label="设备类型">
          <template slot-scope="scope">
            <div v-if="scope.row.paramNo === '100'">
              -
            </div>
            <div v-else>
              {{ scope.row.extAttr2 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="location" label="工单位置"></el-table-column>
        <el-table-column prop="description" label="异常描述" width="260px"></el-table-column>
        <el-table-column prop="gmtCreated" label="生成时间" width="160px"></el-table-column>
        <el-table-column prop="finishTime" label="完成时间" width="160px"></el-table-column>
        <el-table-column prop="feedback" label="处理反馈"></el-table-column>
        <el-table-column label="操作" width="160px">
          <template slot-scope="scope">
            <div v-if="scope.row.status === 100">
              <el-button @click="handleSend(scope.row)" type="text" size="small">派单</el-button>
              <el-button @click="handleCancel(scope.row)" type="text" size="small">取消</el-button>
            </div>
            <el-button @click="handleView(scope.row)" type="text" size="small">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :total="totalPageNum"
        v-if="tableData.length"
      ></el-pagination>
    </el-card>
    <!-- 编辑弹出框 -->
    <work-order-dispatch-modal ref="WorkOrderDispatchModal" @ok="modalOk"></work-order-dispatch-modal>
    <work-order-modal ref="WorkOrderModal" :workOrderStatusData="workOrderStatusData" @ok="modalOk"></work-order-modal>
  </section>
</template>

<script>
import Vue from 'vue'
import { getTypeList, getWorkOrderList, cancelWorkOrder } from './js/api'
import WorkOrderDispatchModal from './modules/WorkOrderDispatchModal'
import WorkOrderModal from './modules/WorkOrderModal'
import { USER_INFO, WorkOrderStatusData, WorkOrderParamData } from '@/views/children/workOrder/js/constants'
import moment from 'moment'
import { checkAuthority } from '@/utils/btnPermission' // 页面按钮权限
export default {
  components: { WorkOrderDispatchModal, WorkOrderModal },
  data () {
    return {
      disabled: true,
      tenantNo: Vue.ls.get(USER_INFO).tenantNo,
      labelWidth: '70px',
      valuetext: 5,
      queryParams: {},
      pageNum: 1,
      pageSize: 10,
      totalPageNum: 10,
      tableData: [],
      dialogFormVisible: true,
      workOrderTypeData: [],
      workOrderStatusData: [],
      tabPosition: ''
    }
  },
  mounted () {
    this.workOrderStatusData = WorkOrderStatusData
    this.getWorkOrderList()
    this.getTypeList()
  },

  methods: {
    checkAuthority,
    formatStatus (row, column, cellValue) {
      return this.$base.constFormatter(WorkOrderStatusData, cellValue)
    },
    formatParam (row, column, cellValue) {
      return this.$base.constFormatter(WorkOrderParamData, Number(cellValue))
    },
    formatTime (row, column, cellValue) {
      if (cellValue) {
        return moment(cellValue).format('YY-MM-DD HH:mm:ss')
      }
    },
    // 工单类型列表
    async getTypeList () {
      let res = await getTypeList(this.tenantNo)
      this.workOrderTypeData = res.data
    },
    onSubmit () {
      this.pageNum = 1
      this.getWorkOrderList()
    },
    async getWorkOrderList () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        status: this.tabPosition
      }
      const data = Object.assign({}, this.queryParams, params)
      let res = await getWorkOrderList(data)
      this.tableData = res.data.list
      this.totalPageNum = res.data.total
    },
    // 派单
    handleSend (row) {
      this.$refs.WorkOrderDispatchModal.isDispatch = true
      this.$refs.WorkOrderDispatchModal.dispatch(row)
    },
    // 取消工单
    handleCancel (row) {
      this.$confirm('您确定取消当前工单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.cancelWorkOrder(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async cancelWorkOrder (row) {
      let res = await cancelWorkOrder(row.no)
      if (res.code === 0) {
        this.$message.success('取消成功!')
        this.getWorkOrderList()
      } else {
        this.$message.error(res.msg)
      }
    },
    // 查看详情
    handleView (row) {
      this.$refs.WorkOrderModal.isDispatch = false
      this.$refs.WorkOrderModal.edit(row)
    },
    // 分页
    pageHandleChange (page) {
      this.pageNum = page
      this.getWorkOrderList()
    },
    modalOk () {
      this.getWorkOrderList()
    },
    handleRadioChange () {
      this.pageNum = 1
      this.getWorkOrderList()
      // this.pageNum = 1
      // this.initData()
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .header-card .el-card__body {
  padding-bottom: 4px;
}
.table-card {
  .el-pagination {
    text-align: center;
    display: block;
    margin: 10px auto;
  }
  /deep/ .el-table th {
    display: table-cell !important;
  }
  /deep/ .el-rate__icon {
    margin-right: 2px;
  }
}
.el-dialog {
  .el-select {
    width: 100%;
  }
}
.tabTop {
  margin: 0 0 20px 0px;
}
</style>
